//
//  ListViewController.m
//  
//
//  Created by 微 醺 on 15/10/6.
//
//

#import "ListViewController.h"
#import "CarSubBrandsCell.h"
#import "SearchModel.h"
#import "CarDetail.h"

@interface ListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ListViewController
- (void)viewWillAppear:(BOOL)animated
{
    AppDelegate *dele = [[UIApplication sharedApplication]delegate];
    if (dele.isNight == 1) {
        self.view.backgroundColor = [UIColor blackColor];
        self.tableView.backgroundColor = [UIColor blackColor];
        [self.tableView reloadData];
    }
    else{
        self.view.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        [self.tableView reloadData];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self tableViewSubView];

}


#pragma mark - 设置tableView
- (void) tableViewSubView
{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = 0;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[CarSubBrandsCell class] forCellReuseIdentifier:@"reuse"];
    
}
#pragma mark - 设置tableView分区数
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arr.count;
    
}

#pragma mark - 设置tableView行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = [self.arr[section] objectForKey:@"series"];
    return arr.count;
    
}

#pragma mark - 设置行高
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SAHeight * 100;
}

#pragma mark - 设置header标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.arr[section] objectForKey:@"subBrandName"];
}

#pragma mark - 设置footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

#pragma mark - 设置header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SAHeight * 30;
}

#pragma mark - 设置tableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CarSubBrandsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];

    NSArray *arr = [self.arr[indexPath.section] objectForKey:@"series"];
    NSDictionary *dic = arr[indexPath.row];
    SearchModel *model = [[SearchModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    cell.model = model;
    
    return cell;
}

#pragma mark - cell选中
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = [self.arr[indexPath.section] objectForKey:@"series"];
    NSDictionary *dic = arr[indexPath.row];
    NSNumber *tempNumber = [dic objectForKey:@"id"];
    
    CarDetail *car = [[CarDetail alloc] init];
    
    car.seriesId = tempNumber.integerValue;
    
    [self.navigationController pushViewController:car animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
